package com.co.video.controller;

import com.co.common.entity.Result;
import com.co.video.pojo.Detail;
import com.co.video.service.DetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@EnableFeignClients(basePackages = "com.co.user.feign")
@RestController
@RequestMapping("/video/detail")

public class DetailController {

    @Autowired
    private DetailService detailService;

   /* @GetMapping("/{id}")
    public Result<Object> findByUsername(@PathVariable String id){
        Detail detail = detailService.findByUsername(id);
        return Result.ok(detail);
    }*/


    /**
     * TvVdetail分页条件搜索实现
     *
     * @param detail
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Detail>> findPage(@RequestBody(required = false) Detail detail, @PathVariable int page, @PathVariable int size) {
        //调用TvVdetailService实现分页条件查询TvVdetail
        PageInfo<Detail> pageInfo = detailService.findPage(detail, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * TvVdetail分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Detail>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用TvVdetailService实现分页查询TvVdetail
        PageInfo<Detail> pageInfo = detailService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param detail
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Detail>> findList(@RequestBody(required = false) Detail detail) {
        //调用TvVdetailService实现条件查询TvVdetail
        List<Detail> list = detailService.findList(detail);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用TvVdetailService实现根据主键删除
        detailService.delete(id);
        return Result.ok();
    }

    /**
     * 修改TvVdetail数据
     *
     * @param detail
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@RequestBody Detail detail, @PathVariable String id) {
        //设置主键值
        detail.setAid(id);
        //调用TvVdetailService实现修改TvVdetail
        detailService.update(detail);
        return Result.ok();
    }

    /**
     * 新增TvVdetail数据
     *
     * @param detail
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody Detail detail) {
        //调用TvVdetailService实现添加TvVdetail
        detailService.add(detail);
        return Result.ok();
    }

    /**
     * 根据ID查询TvVdetail数据
     *
     * @param aid
     * @return
     */
    @GetMapping("/{aid}")
    public Result<Detail> findById(@PathVariable String aid) {
        //调用TvVdetailService实现根据主键查询TvVdetail
        Detail detail = detailService.findById(aid);
        return Result.ok(detail);
    }

    /**
     * 查询TvVdetail全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Detail>> findAll() {
        //调用TvVdetailService实现查询所有TvVdetail
        List<Detail> list = detailService.findAll();
        return Result.ok(list);
    }

    /**
     * 根据用户名查询上传视频
     *
     * @param username
     * @return
     */
    @GetMapping("/user/{username}")
    public Result<Object> findListByUsername(@PathVariable("username") String username) {
        if (username == null) {
            return Result.failed();
        }
        List<Map> listByUsername = detailService.findListByUsername(username);
        return Result.ok(listByUsername);
    }


}
